package com.topc.problem.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.topc.base.exception.Asserts;
import com.topc.base.model.PageResult;
import com.topc.base.utils.PageUtils;
import com.topc.problem.mapper.ProblemDealCommentInfoMapper;
import com.topc.problem.model.dto.SaveDealCommentDto;
import com.topc.problem.model.po.ProblemDealCommentInfo;
import com.topc.problem.service.IProblemDealCommentInfoService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class ProblemDealCommentInfoServiceImpl extends ServiceImpl<ProblemDealCommentInfoMapper, ProblemDealCommentInfo> implements IProblemDealCommentInfoService {
    @Override
    public PageResult<ProblemDealCommentInfo> searchByPage(int pageSize, int pageCurrent, long problemDealId,String userNumber) {
        LambdaQueryWrapper<ProblemDealCommentInfo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(!ObjectUtil.isEmpty(problemDealId),ProblemDealCommentInfo::getProblemDealId,problemDealId)
                .eq(!ObjectUtil.isEmpty(userNumber),ProblemDealCommentInfo::getUserNumber,userNumber);
        return PageUtils.pageToResult(this.getBaseMapper().selectPage(PageUtils.toPageBuild(pageSize, pageCurrent),lambdaQueryWrapper));
    }

    @Override
    public boolean deleteByQuestionDealInfoIds(Long[] ids) {
        return false;
    }

    @Override
    public void saveCommentInfo(SaveDealCommentDto saveDealCommentDto) {
        ProblemDealCommentInfo problemDealCommentInfo = new ProblemDealCommentInfo();
        BeanUtil.copyProperties(saveDealCommentDto, problemDealCommentInfo);
        boolean save = this.save(problemDealCommentInfo);
        if (!save) Asserts.fail("评论失败");
    }

    @Override
    public void deleteCommentInfoById(Long id) {
        boolean remove = this.removeById(id);
        if (!remove) Asserts.fail("删除失败");
    }


//
//    @Override
//    @Transactional(rollbackFor = Exception.class)
//    public boolean deleteByProblemDealInfoIds(Long[] ids) {
//        boolean flag = true;
//        for(int i = 0;i < ids.length;i++){
//            LambdaQueryWrapper<ProblemDealCommentInfo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
//            try {
//                lambdaQueryWrapper.eq(ids[i] != null, ProblemDealCommentInfo::getProblemDealId, ids[i]);
//                if (this.getBaseMapper().delete(lambdaQueryWrapper) == 0) {
//                    flag = false;
//                    throw new Exception("This is a delete-by-problem-deal-info-ids exception.");
//                }
//            }catch (Exception e){
//                log.error("Failed to delete record with ID: " + ids[i], e);
//            }
//        }
//
//        return flag;
//    }
}
